var contextPath = document.location.pathname.substr(0, document.location.pathname.substr(1).indexOf("/") + 1);
/**
 * 应用基本信息表单验证
 * @param url
 */
function checkAppForm(formId){
	 	$("#"+formId).validate({
	 		onfocusout: function (element, event) {
	 		    if (event.which === 9 && this.elementValue(element) === "") {
	 		        return;
	 		    } else {
	 		        this.element(element);
	 		    }
	 		},
	 		rules: {
	 			webName: {
		 		    required: true,
		 		    remote:{       //验证用户名是否存在
		               type:"POST",
		               url:contextPath+"/portal/web/checkWebName",             
		               data:{webName:function(){
		            	   			return $("#webName").val();
		            	   			},
	 						webNameNow:function(){
		 							if(document.getElementById("webNameNow")!=undefined){
		 								return $("#webNameNow").val();
	 								}else{
	 									return "";
	 								}
	 							},
		               		appId:function(){
			               			if(document.getElementById("appId")!=undefined){
		 								return $("#appId").val();
	 								}else{
	 									return "";
	 								}
		               			}
		               		} 
		              } 
	            },
	            webDomains: {
		 		    required: true,
		 		    isDomain:true
	 		   }
 		    },
	        messages: {
	        	webName: {
	        		required: "请输入网站名称",
	        		remote: "应用名已存在"
	        	},
	        	webDomains: {
	        		required: "请输入网站域名",
	        	}
 		   },
	        submitHandler:function(form){
	        	$(form).ajaxSubmit({
	        		type: "post",
	                url:contextPath+"/portal/web/saveAndCheckApp",   
	                data:$(form).serialize(),
	                async: false,
	                datatype:"json",
	                success: function(response) {
	                	if(response.status){
			        		  bootboxAlert("验证成功","验证网站所有权通过",response.status);
			        	  }else{
			        		  bootboxAlert("验证失败","验证网站所有权未通过",response.status);
			        	  }
	                }
	              });
	        }
	    });
	 	$("#reset").click(function() {
            validator.resetForm();
        });
	};
	
	function bootboxAlert(title,mess,status) {
		var html = "<div style='text-align:center;'>"+mess+"<br/></div>";
		bootbox.dialog({
	    	title : title,
	    	message : html,
	    	buttons : {
	    		"success" : {
	    			"label" : "<i class='icon-ok'></i> 确定",
	    			"className" : "btn-sm btn-success",
	    			"callback" : function() {
	    				if(status){
	    					window.location.href = contextPath+"/portal/cooperator/MyApp";
	    				}
	    			}
	    		},
	    	},
	    	className: 'myStyle'
	    });
	}
	
	/**
	 * 域名校验
	 */
	jQuery.validator.addMethod("isDomain", function(value,element) {
 		var domain =/^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/;
 		return this.optional(element) || domain.test(value);
		}, "请正确输入网站域名");
	
	/**
	 * 删除应用
	 */
	function deletApp(appId){
		var html = "<div style='text-align:center;'>确认要删除该应用？<br/></div>";
		bootbox.dialog({
	    	title : "提示",
	    	message : html,
	    	buttons : {
	    		"success" : {
	    			"label" : "<i class='icon-ok'></i> 确定",
	    			"className" : "btn-sm btn-success",
	    			"callback" : function() {
	    				$.ajax({
	    		    		type: "post",
	    		            url:contextPath+"/portal/web/deletApp",   
	    		            data:{"appId":appId},
	    		            async: false,
	    		            datatype:"json",
	    		            success: function(response) {
	    		            	if(response.status){
	    			        		  bootboxAlert("提示","删除成功",response.status);
	    			        	  }else{
	    			        		  bootboxAlert("提示","删除失败",response.status);
	    			        	  }
	    		            }
	    		          });
	    			}
	    		},
	    		"cancel" : {
	    			"label" : "<i class='icon-info'></i> 取消",
	    			"className" : "btn-sm btn-danger",
	    			"callback" : function() {
	    			}
	    		}
	    	},
	    	className: 'myStyle'
	    });
	}